<template>
  <el-dialog title="选择项目" :visible.sync="visible" width="1000px" append-to-body>
    <!-- 查询表单 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true">
      <el-form-item label="项目编号" prop="projectNumber">
        <el-input
          v-model="queryParams.projectNumber"
          placeholder="请输入项目编号"
          clearable
          style="width: 180px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="项目名称" prop="projectName">
        <el-input
          v-model="queryParams.projectName"
          placeholder="请输入项目名称"
          clearable
          style="width: 180px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="合同名称" prop="contractName">
        <el-input
          v-model="queryParams.contractName"
          placeholder="请输入合同名称"
          clearable
          style="width: 180px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 项目列表 -->
    <el-table
      v-loading="loading"
      :data="projectList"
      @row-click="handleProjectSelect"
      highlight-current-row
      border
      style="width: 100%"
    >
      <el-table-column label="合同名称" prop="contractName" align="center" min-width="180" />
      <el-table-column label="项目名称" prop="projectName" align="center" min-width="180" />
      <el-table-column label="项目编号" prop="projectNumber" align="center" min-width="120" />
      <el-table-column label="产品名称" prop="productName" align="center" min-width="180" />
      <el-table-column label="项目负责人" prop="projectLeader" align="center" min-width="100" />
    </el-table>

    <!-- 分页 -->
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getProjectList"
    />
  </el-dialog>
</template>

<script>
import { listApproved, getProjectInfo } from '@/api/project/planning'

export default {
  name: 'SelectProject',
  data() {
    return {
      // 遮罩层
      loading: false,
      // 显示状态
      visible: false,
      // 项目列表
      projectList: [],
      // 总条数
      total: 0,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 15,
        projectNumber: null,
        projectName: null,
        contractName: null
      }
    }
  },
  methods: {
    /** 显示对话框 */
    show() {
      this.visible = true
      this.getProjectList()
    },
    /** 隐藏对话框 */
    hide() {
      this.visible = false
    },
    /** 获取项目列表 */
    getProjectList() {
      this.loading = true
      listApproved(this.queryParams).then(response => {
        this.projectList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getProjectList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    /** 选择项目回调 */
    handleProjectSelect(project) {
      getProjectInfo(project.projectId).then(response => {
        this.$emit('select', response.data)
        this.hide()
      })
    }
  }
}
</script>
